
$(function () {
    var pageContextVal = $("#PageContext").val();
    var wxCodeVal = $("#wxCode").val();
    var windowW = $(window).width();
    var windowH = $(window).height();
    if (windowW > 600) {
        windowW = 600;
    }

    var topH = $(".userCheckCont").height();
    $(".solidCont").css({"height": (windowH - topH) + "px"});
    $(".infoCheckEditCont").css({"height": (windowH - topH - 30) + "px"});

    // cover
    var contentH = $(".content").height();
    $(".imageCover").css({"height": contentH + "px"});

    // 样例图片展示  501*377
    var imageH = windowW * 377 / 501;
    $(".coverCont").css({"margin-top": (windowH - imageH) / 2 + "px"});

    $(".imageCover").click(function () {
        $(".imageCover").fadeOut();
    });

    $(".solidCont").css({"margin-left": 0 + "px"});

    $(".userRegBtn").click(function () {
        $(".solidCont").animate({"margin-left": -windowW + "px"}, "fast");
    });

    $(".backBtn").click(function () {
        $(".solidCont").animate({"margin-left": 0 + "px"}, "fast");
    });

    var $show = $("#show");
    var $showPhone = $("#showPhone");
    var $text = $("#text");
    var $hide = $("#hide");
    var $img = $("#img");
    var $firsrBtn = $("#firsrBtn");
    var $phone = $("#phone");
    var phoneNumber;
    $hide.hide();
    var windowId1 = layer.load();
    $.getJSON("/wx/isUserCheckStart.do", null, function(data) {
        layer.close(windowId1);
        $text.text(data.message);
        if (data.status >= 0) {
            phoneNumber = data.data.phone;
            $show.hide();
            $showPhone.text(data.data.phone);
            $hide.show();
            $img.attr("src", "./images/ok.png");
            $firsrBtn.text("更改绑定手机");
            $phone.attr("placeholder", "请输入新的手机号码...");
        } else if (data.status === -404) {
            location.assign('/login.html');
        }
    });

    var $msg = $("#msg");
    var $labelPhone = $(".inputTitle:eq(1)");
    var $labelMsg = $(".inputTitle:eq(3)");
    var $sendMessage = $(".verifyBtn");
    var $submit = $("#userRegSubmitBtn");
    $msg.css('width', '100px');
    $phone     .blur(function () {test_ipt(testPhone, $phone, $labelPhone);});
    $msg       .blur(function () {test_ipt(/^\d{6}$/, $msg, $labelMsg);});

    var sendMessage = function () {
        if (!test_ipt(testPhone, $phone, $labelPhone)) {
            layer.msg("手机号码不正确");
            $phone.focus();
            return false;
        }
        if (phoneNumber === $phone.val()) {
            layer.msg("手机号码已验证");
            $phone.focus();
            return false;
        }
        waitingForNextClick($sendMessage, 60, '获取验证码');
        var windowId = layer.load();
        $.getJSON("/wx/sendMessage.do", {phone: $phone.val()}, function (data) {
            layer.close(windowId);
            layer.msg(data.message);
            if (data.status === -404) {
                location.assign('/login.html');
            }
        });
    };
    var submit = function () {
        if (!test_ipt(testPhone, $phone, $labelPhone)) {
            layer.msg("手机号码不正确");
            $phone.focus();
            return false;
        }
        if (phoneNumber === $phone.val()) {
            layer.msg("手机号码已验证");
            $phone.focus();
            return false;
        }
        if (!test_ipt(/^\d{6}$/, $msg, $labelMsg)) {
            layer.msg("验证码错误");
            $msg.focus();
            return false;
        }
        var load = layer.load();
        $.post("/wx/userVerify.do", {phone: $phone.val(),msg: $msg.val()}, function (data) {
            layer.close(load);
            layer.msg(data.message);
            if (data.status >= 0) {
                phoneNumber = $phone.val();
                $showPhone.text($phone.val());
                $phone.val('');
                $msg.val('');
                $img.attr("src", "images/ok.png");
                $text.text('已认证');
                $show.hide();
                $hide.show();
                $firsrBtn.text("更改绑定手机");
                $(".backBtn").click();
                $phone.attr("placeholder", "请输入新的手机号码...");
            }
        });
    };
    $sendMessage.width($sendMessage.width());
    $sendMessage.click(sendMessage);
    $submit.click(submit);
    setEnterKeyEvent($phone, sendMessage);
    setEnterKeyEvent($msg, submit);
});